﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;

namespace Validate_Solution
{
    public class Validater
    {
        ArrayList validaters;
        ErrorManager errorManager;
        Color requiredColor = Color.LightBlue;
        Dictionary<string, Button> buttons = new Dictionary<string, Button>();
        IValidate ivalidate;

        public Validater()
        {
            validaters = new ArrayList();
            errorManager = new ErrorManager();
        }

        public Validater(IValidate validate)
        {
            validaters = new ArrayList();
            errorManager = new ErrorManager();
            ivalidate = validate;
        }

        public Color ErrorColor
        {
            get { return errorManager.ErrorColor; }
            set { errorManager.ErrorColor = value; }
        }

        public Color RequiredColor
        {
            get { return requiredColor; }
            set { requiredColor = value; }
        }
        /// <summary>
        /// 对指定控件添加验证错误信息
        /// </summary>
        /// <param name="control"></param>
        /// <param name="requiredErrorMessage"></param>
        public void AddRequiredTextItem(Control control, string requiredErrorMessage)
        {
            validaters.Add(new TextValidater(true, null, control, requiredErrorMessage, null));
            control.BackColor = requiredColor;
        }
        /// <summary>
        /// 对指定控件添加正则验证错误信息
        /// </summary>
        /// <param name="control"></param>
        /// <param name="requiredErrorMessage"></param>
        /// <param name="regex"></param>
        /// <param name="regxErrorMessage"></param>
        public void AddRequiredTextItem(Control control, string requiredErrorMessage, string regex, string regxErrorMessage)
        {
            validaters.Add(new TextValidater(true, regex, control, requiredErrorMessage, regxErrorMessage));
            control.BackColor = requiredColor;
        }
        /// <summary>
        /// 对指定控件添加正则验证错误信息
        /// </summary>
        /// <param name="control"></param>
        /// <param name="regex"></param>
        /// <param name="regxErrorMessage"></param>
        public void AddUnRequiredTextItem(Control control, string regex, string regxErrorMessage)
        {
            validaters.Add(new TextValidater(false, regex, control, null, regxErrorMessage));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="validateExecuter"></param>
        public void AddCustomValidater(ExecuteValidate validateExecuter)
        {
            validaters.Add(new CustomValidater(validateExecuter));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="errorMessage"></param>
        /// <returns></returns>
        public bool Validate(out string errorMessage)
        {
            errorMessage = null;
            Control[] errorControls = null;
            foreach (UIValidater validater in validaters)
            {
                if (!validater.Validate(out errorMessage, out errorControls))
                {
                    errorManager.SetErrors(errorControls);
                    errorControls[0].Focus();
                    return false;
                }
            }
            errorManager.ClearError();
            return true;
        }
        /// <summary>
        /// 添加按钮
        /// </summary>
        /// <param name="btn"></param>
        private void AddButton(Button btn)
        {
            if (!buttons.ContainsKey(btn.Name))
            {
                buttons.Add(btn.Name, btn);
                btn.Click -= new EventHandler(btn_Click);
                btn.Click += new EventHandler(btn_Click);
            }
        }
        /// <summary>
        /// 验证事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btn_Click(object sender, EventArgs e)
        {
            ivalidate.DoValidate();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="errorMessage"></param>
        /// <param name="errorControl"></param>
        /// <returns></returns>
        public bool Validate(out string errorMessage, out Control errorControl)
        {
            errorControl = null;
            errorMessage = null;
            Control[] errorControls = null;
            foreach (UIValidater validater in validaters)
            {
                if (!validater.Validate(out errorMessage, out errorControls))
                {
                    errorManager.SetErrors(errorControls);
                    errorControls[0].Focus();
                    errorControl = errorControls[0];
                    return false;
                }
            }
            errorManager.ClearError();
            return true;
        }
        /// <summary>
        /// 
        /// </summary>
        public void ClearError()
        {
            errorManager.ClearError();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="control"></param>
        /// <param name="toolTip"></param>
        /// <param name="messageTitle"></param>
        /// <param name="err"></param>
        public void Show(Control control, ref ToolTip toolTip, string messageTitle, string err)
        {
            if (toolTip != null)
            {
                toolTip.RemoveAll();
                toolTip.Dispose();
            }
            toolTip = new ToolTip();
            toolTip.ToolTipIcon = ToolTipIcon.Warning;
            toolTip.IsBalloon = true;
            toolTip.ToolTipTitle = messageTitle;
            toolTip.AutoPopDelay = 0x1388;
            
            if (err != null)
            {
                int length = err.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries).Length;
                toolTip.Show(err, control, new Point(10, (-47 - (length * 0x12)) + ((control.Height - 0x15) / 2)));
                return;
            }
            toolTip.Show("", control, new Point(10, -65 + ((control.Height - 0x15) / 2)));
        }
    }
}
